# -*- coding: utf-8 -*-
# @Time    : 2020/3/14 下午1:07
# @Author  : lilong

from abc import ABCMeta, abstractmethod
from enum import Enum


# ==============================
# 代码框架：工厂方法
# ==============================

# ------------主要是定义产品类：抽象类+继承-----------
class PenType(Enum):
    """画笔类型：enmu类用于定义一些固定不变的常量
    """
    PenTypeLine = 1
    PenTypeRect = 2
    PenTypeEllipse = 3


class Pen(metaclass=ABCMeta):
    """产品抽象类"""

    def __init__(self, name):
        self.__name = name

    def getName(self):
        return self.__name

    @abstractmethod
    def getType(self):
        pass


class LinePen(Pen):
    """产品类：直线画笔"""

    def __init__(self, name):
        super().__init__(name)

    def getType(self):
        return PenType.PenTypeLine


class RectanglePen(Pen):
    """产品类：矩形画笔"""

    def __init__(self, name):
        super().__init__(name)

    def getType(self):
        return PenType.PenTypeRect


class EllipsePen(Pen):
    """产品类：椭圆画笔"""

    def __init__(self, name):
        super().__init__(name)

    def getType(self):
        return PenType.PenTypeEllipse


class PenFactory:
    """工厂类：
    返回指定的产品类对象
    """

    def __init__(self):
        """定义一个字典(key:PenType，value：Pen)来存放对象，确保每一个产品类只会有一个对象"""
        self.__pens = {}

    def getSingleObj(self, penType, name):
        """获得唯一实例的对象"""
        pass

    def createPen(self, penType):
        """创建画笔"""

        # 如果该对象不存在，则创建一个对象并存到字典中
        if self.__pens.get(penType) is None:
            if penType == PenType.PenTypeLine:
                pen = LinePen("直线画笔")
            elif penType == PenType.PenTypeRect:
                pen = RectanglePen("矩形画笔")
            elif penType == PenType.PenTypeEllipse:
                pen = EllipsePen("椭圆画笔")
            else:
                pen = Pen("")
            self.__pens[penType] = pen

        # 否则直接返回字典中的对象
        return self.__pens[penType]


# ==============================
# 基于框架的实现
# ==============================
def testPenFactory():
    factory = PenFactory()
    linePen = factory.createPen(PenType.PenTypeLine)
    print("创建了 %s，对象id：%s， 类型：%s" % (linePen.getName(), id(linePen), linePen.getType()))
    rectPen = factory.createPen(PenType.PenTypeRect)
    print("创建了 %s，对象id：%s， 类型：%s" % (rectPen.getName(), id(rectPen), rectPen.getType()))
    rectPen2 = factory.createPen(PenType.PenTypeRect)
    print("创建了 %s，对象id：%s， 类型：%s" % (rectPen2.getName(), id(rectPen2), rectPen2.getType()))
    ellipsePen = factory.createPen(PenType.PenTypeEllipse)
    print("创建了 %s，对象id：%s， 类型：%s" % (ellipsePen.getName(), id(ellipsePen), ellipsePen.getType()))


# testCoffeeMaker()
testPenFactory()
